package com.fingard.dsp.bank.directbank.bjrc01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.math.BigDecimal;

/**
 * @author Parker on 2020-03-12.
 * @version 1.0
 */
    public class BJRC01SinglePay extends BJRC01Base implements ISinglePay {

    /**
     * 支付
     * @param p_req1908
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接请求报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            BJRC01Header header=new BJRC01Header();
            header.tr_code="300001";
            header.req_no = p_req1908.singleDetail.askNumber;
            writeCommonHeader(xmlTextWriter,header);
            //拼接请求报文体
            XElement body = new XElement("body","");
            body.addChild(new XElement("pay_acno",p_req1908.singleDetail.payAct));//付款账号
            body.addChild(new XElement("pay_cur_code",convertCurToBank(p_req1908.singleDetail.payCur)));//付款币种
            body.addChild(new XElement("pay_acname",p_req1908.singleDetail.payName));//付款户名
            body.addChild(new XElement("as_flag",""));//账簿标志
            body.addChild(new XElement("as_acno",""));//账簿号
            body.addChild(new XElement("as_acname",""));//账簿名称
            body.addChild(new XElement("pay_accaddr",""));//付款地址
            body.addChild(new XElement("cert_type",""));//凭证种类
            body.addChild(new XElement("cert_no",p_req1908.singleDetail.askNumber));//凭证号
            body.addChild(new XElement("rcv_acno",p_req1908.singleDetail.recAct));//收款账号
            body.addChild(new XElement("rcv_cur_code",convertCurToBank(p_req1908.singleDetail.recCur)));//收款币种
            body.addChild(new XElement("rcv_acname",p_req1908.singleDetail.recName));//收款户名
            body.addChild(new XElement("rcv_accaddr",p_req1908.singleDetail.recAreaName));//收款人地址
            body.addChild(new XElement("rcv_bank_no",p_req1908.singleDetail.cnapsCode));//收款行号
            body.addChild(new XElement("rcv_bank_name",p_req1908.singleDetail.cnapsName));//收款行名
            body.addChild(new XElement("amt",p_req1908.singleDetail.payAmount));//交易金额
            body.addChild(new XElement("bank_flag","1".equals(p_req1908.singleDetail.sameBnk)?"0":"1"));//银行标志
            String urgencyFlag=p_req1908.singleDetail.paySpd;//加急标识
            if("0".equals(p_req1908.singleDetail.sameBnk)){
                String urgentFlag = getBankConfig().getValueAsString("UrgentLargeAmount");
                if (StringHelper.hasAnyChar(urgentFlag)) {
                    if ((new BigDecimal(p_req1908.singleDetail.payAmount)).compareTo(new BigDecimal(urgentFlag)) >= 0) {
                        urgencyFlag="1";
                    }
                }
            }
            body.addChild(new XElement("urgency_flag",urgencyFlag));//加急标志
            body.addChild(new XElement("area_flag","1".equals(p_req1908.singleDetail.sameCity)?"0":"1"));//同城异地标志
            body.addChild(new XElement("saverecvinfo_flag",""));//保存收款方信息
            body.addChild(new XElement("booking_flag","0"));//预约标志
            body.addChild(new XElement("booking_date",""));//预约日期
            body.addChild(new XElement("booking_time",""));//预约时间
            body.addChild(new XElement("delay_flag",""));//支付系统关门后是否自动转预约
            body.addChild(new XElement("mobiles",""));//手机号
            body.addChild(new XElement("bank_kind",""));//银行类别
            body.addChild(new XElement("purpose",p_req1908.singleDetail.purpose));//用途
            body.addChild(new XElement("postscript",p_req1908.singleDetail.memo));//附言
            nodeAnalysis(p_req1908.singleDetail.srcXElement,body,getCharset());
            xmlTextWriter.writeXElement(body);
            xmlTextWriter.writeEndDocument();

            //发送请求报文
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), p_req1908.reqHeader.transType, p_req1908.reqHeader.commandCode);
            //处理响应报文
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1].substring(bankResult[1].indexOf("<ap>")), tmpSbMsg);
                if(xmlSbReader!=null){
                    String succFlag= xmlSbReader.getFirstText("/ap/head/succ_flag");
                    String respCode = xmlSbReader.getFirstText("/ap/head/ret_code");
                    String respInfo = xmlSbReader.getFirstText("/ap/head/ret_info");
                    detail.respCode = "0";
                    detail.respInfo = "返回状态码："+succFlag+"+"+respCode+"返回结果："+respInfo;
                    detail.reqSeqID = p_req1908.singleDetail.askNumber;
                    if("0".equals(succFlag)){//交易成功：处理中
                        detail.transState = succFlag;
                    }else {
                        detail.transState = respCode;//9999、1102结果未知
                    }
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }
    /**
     * 查询支付结果
     * @param p_req1944
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            BJRC01Header header=new BJRC01Header();
            header.tr_code="200205";
            header.req_no=p_req1944.askNumber;
            writeCommonHeader(xmlTextWriter,header);
            //拼接报文体
            XElement body = new XElement("body","");
            body.addChild(new XElement("cert_no",""));//企业ERP[账务]流水号
            body.addChild(new XElement("req_no",p_req1944.askNumber));//企业ERP［请求］流水号
            body.addChild(new XElement("tr_acdt", DateHelper.convertD10toD8(p_req1944.bgnDate)));//交易日期
            body.addChild(new XElement("cms_corp_no", ""));//客户号 前置机自己赋值 企业接口可没有

            xmlTextWriter.writeXElement(body);
            xmlTextWriter.writeEndDocument();

            //发送请求报文
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), p_req1944.reqHeader.transType, p_req1944.reqHeader.commandCode);
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1].substring(bankResult[1].indexOf("<ap>")), tmpSbMsg);
                if (xmlSbReader != null) {
                    String succFlag= xmlSbReader.getFirstText("/ap/head/succ_flag");
                    String respCode = xmlSbReader.getFirstText("/ap/head/ret_code");
                    String respInfo = xmlSbReader.getFirstText("/ap/head/ret_info");
                    if ("0".equals(succFlag) && "0000".equals(respCode)) {
                        //交易结果
                        detail.respCode = "0";
                        detail.respInfo = respInfo;
                        detail.payInfoCode =xmlSbReader.getFirstText("/ap/body/stat");
                        detail.transState = detail.payInfoCode;
                        detail.reqSeqID = xmlSbReader.getFirstText("/ap/body/cert_no");
                    }else if("2".equals(succFlag) && "2100".equals(respCode)){
                        //交易结果
                        detail.respCode = "0";
                        detail.respInfo = respInfo;
                        detail.payInfoCode = respCode;
                        detail.transState = detail.payInfoCode;
                        detail.reqSeqID = xmlSbReader.getFirstText("/ap/body/cert_no");
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = "返回状态码："+succFlag+"+"+respCode+"返回结果："+respInfo;
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
